package com.cc.gulimall.product.config;

import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * <p>Title: MyCacheConfig</p>
 * Description：redis配置
 * date：2020/6/12 16:38
 */
@EnableConfigurationProperties(CacheProperties.class)
@EnableCaching
@Configuration
public class MyCacheConfig {


    /**
     * 我们的配置文件中 TTL设置没用上
     * <p>
     * CacheProperties.class和配置文件绑定的配置类是这样的:
     *
     * @ConfigurationProperties(prefix = "spring.cache")
     * public class CacheProperties
     * <p>
     * 现在要让这个配置文件生效	: @EnableConfigurationProperties(CacheProperties.class)
     */
    @Bean
//	方法中的参数自动从容器中寻找引入，这个类没自动在容器中，我们手动绑定后也可以找到 ：@EnableConfigurationProperties(CacheProperties.class)
    RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
//		链式调用，自己返回自己类型新对象，修改一些想修改的属性，老值加新值
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();

        // 设置kv的序列化机制，改为json
        config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        CacheProperties.Redis redisproperties = cacheProperties.getRedis();

        // 设置配置
        if (redisproperties.getTimeToLive() != null) {
            config = config.entryTtl(redisproperties.getTimeToLive());
        }
        if (redisproperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisproperties.getKeyPrefix());
        }
        if (!redisproperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisproperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
    }

}
